package org.fnzn.web.fngl;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.fnzn.aop.SystemControllerLog;
import org.fnzn.bean.FnglTurnover;
import org.fnzn.core.Page;
import org.fnzn.core.Result;
import org.fnzn.service.FnglTurnoverService;
import org.fnzn.util.DateTools;
import org.fnzn.util.ExcelUtil;
import org.fnzn.web.SysLogController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 *成交记录信息管理
 */
@RestController
@RequestMapping(value = "/fnglTurnover")
@EnableAutoConfiguration
public class FnglTurnoverContoller {
    Logger log = Logger.getLogger(SysLogController.class);

    @Autowired
    private FnglTurnoverService fnglTurnoverService;

    @RequestMapping(value = "/list",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    @SystemControllerLog(description="查询成交记录")
    public Result findFnglTurnover(FnglTurnover fnglTurnover, Page page){
        return fnglTurnoverService.findFnglTurnover(fnglTurnover,page);
    }

    @RequestMapping(value = "/add",produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
    @SystemControllerLog(description="新增成交记录")
    public Result insertFnglTurnover(FnglTurnover fnglTurnover){
        return  fnglTurnoverService.insertFnglTurnover(fnglTurnover);
    }

    @RequestMapping(value = "/update",produces = "application/json;charset=UTF-8",method = RequestMethod.PUT)
    @SystemControllerLog(description="修改成交记录")
    public Result updateFnglTurnover(FnglTurnover fnglTurnover){
        return  fnglTurnoverService.updateFnglTurnover(fnglTurnover);
    }

    @RequestMapping(value = "/{id}",produces = "application/json;charset=UTF-8",method = RequestMethod.DELETE)
    @SystemControllerLog(description="删除成交记录")
    public Result deleteFnglTurnover(@PathVariable String id){
        return  fnglTurnoverService.deleteFnglTurnover(id);
    }

    @RequestMapping(value = "/deleteAll",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    @SystemControllerLog(description="删除多个成交记录")
    public Result deleteAllFnglTurnover(String Ids){
        return fnglTurnoverService.deleteAllFnglTurnover(Ids);
    }

    /**
     *  成交点击上一步下一步进度改变佣金改变
     */
    @RequestMapping(value = "/nextStep/{Id}/{flag}",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    @SystemControllerLog(description="成交下一步或者上一步")
    public Result nextStep(@PathVariable String Id,@PathVariable String flag){
        return fnglTurnoverService.nextStep(Id,flag);
    }

    /**
     *  成交点击上一步下一步进度改变佣金改变
     */
    @RequestMapping(value = "/nextStep1/{Id}/{flag}",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    @SystemControllerLog(description="成交下一步或者上一步")
    public Result nextStep1(@PathVariable String Id,@PathVariable String flag){
        return fnglTurnoverService.nextStep1(Id,flag);
    }



    /**
     * 导出报表
     * @return
     */
    @RequestMapping(value = "/export",produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
    public void export(HttpServletRequest request, HttpServletResponse response,FnglTurnover fnglTurnover, Page page) throws Exception {
        //获取数据
        Result result = findFnglTurnover(fnglTurnover, page);
        Page newPage = (Page) result.getData();
        List<?> rows = newPage.getRows();
        List<FnglTurnover> list = (List<FnglTurnover>) rows;


        //excel标题
         String[] title = {"销售姓名","客户姓名","客户电话","其他销售","项目名称","房号","付款方式","结算","佣金","结算日期","当前进度"};

         //excel文件名
         String fileName = "成交信息表"+DateTools.formatDate(new Date())+".xls";

        //sheet名
         String sheetName = "成交信息表";

        String[][] content;
             content = new String[list.size()][title.length] ;
             for (int i = 0; i < list.size(); i++) {
                 FnglTurnover fnglTurnover1 = list.get(i);
//                 content[i][0] = fnglTurnover1.getStaffName();
//                 content[i][1] = fnglTurnover1.getCustomerName();
                 content[i][2] = fnglTurnover1.getCustomerPhone();
                 content[i][3] = fnglTurnover1.getOtherStaffName();
                 content[i][4] = fnglTurnover1.getProjectId();
                 content[i][5] = fnglTurnover1.getRoomNumber();
                 content[i][6] = fnglTurnover1.getPayType();
                 content[i][7] = fnglTurnover1.getPaymentFlag();
                 content[i][8] = fnglTurnover1.getAmount();
                 content[i][9] = DateTools.formatDate(fnglTurnover1.getCreateTime());
                 content[i][10] = fnglTurnover1.getStep();
             }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            //os.write(wb.getBytes( ));
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //响应到客户端
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            /*try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }*/
            fileName = URLEncoder.encode(fileName,"UTF-8");
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            /*response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");*/
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
